/**
 * Date Service
 * 日期服务， 提供对日期的相关操作
 */

angular.module('JiangGanApp')
  .service('DateService', DateService);

function DateService() {
  //根据选项获取时间区域
  this.getTimeByChoice = function(choice){
    var today = new Date();
    var lastyear = today.getFullYear()-1;
    var lasttoday = today.setYear(lastyear)
    if(choice=='yesterday'){
      return [today - 1 , today - 1];
    }else if(choice=='week'){
      var start = today - 7;
      var end = today;
      return[start,end]  
    }else if(choice=='month'){
      var start = today - 30;
      var end = today;
      return[start,end]
    }else if(choice=='lasttoday'){
      return [lasttoday,lasttoday]
    }else if(choice=='lastyesterday'){
      return [lasttoday-1 ,lasttoday - 1]
    }else if(choice=='lastweek'){
      return [lasttoday-7,lasttoday]
    }else if(choice=='lastmonth'){
      return [lasttoday-30,lasttoday]
    }else if(choice=='today'){
      return [today,today]
    }
  }
  
  //获取当前月第一天到最后一天
  this.getCurrentMonthSE = function() {
    var today = new Date();
    today.setDate(1);
    var start = today.format('yyyy-MM-dd');
    today.setMonth(today.getMonth() + 1);
    today.setDate(today.getDate() - 1);
    var end = today.format('yyyy-MM-dd');
    return [start, end];
  }

  //获取当前星期的第一天到最后一天
  //星期日为起始日
  this.getCurrentWeekSE = function() {
    var today = new Date();
    today.setDate(today.getDate() - today.getDay());
    var start = today.format('yyyy-MM-dd');
    today.setDate(today.getDate() + 6);
    var end = today.format('yyyy-MM-dd');
    return [start, end];
  }
  //获取当前星期的每一天
  this.getCurrentWeek = function(){
    var today = new Date();
    today.setDate(today.getDate() - today.getDay()-1);
    var start = today.format('M月d日')
    var result = [];
    for(var i=0 ;i<7;i++){
      today.setDate(today.getDate() + 1);
      var day = today.format('M月d日')
      result.push(day)
    }
    return result;
  }
  //放入时间，返回时间差
  this.getTimeDif = function(time){
      var now = new Date();
      var date = new Date(time);
      var timeDif = now - date;
      //计算相差年数
      var year = now.getFullYear() - date.getFullYear();
      //计算相差月数
      var month = (now.getMonth() - date.getMonth())>-1?(now.getMonth() - date.getMonth()):(now.getMonth() - date.getMonth()+12);
      //计算出相差天数
      var days=Math.floor(timeDif/(24*3600*1000))
      //计算出小时数  
      var hour=timeDif%(24*3600*1000)    
      //计算天数后剩余的毫秒数
      var hours=Math.floor(hour/(3600*1000))
      //计算相差分钟数
      var min=hour%(3600*1000)
      //计算小时数后剩余的毫秒数
      var minutes=Math.floor(min/(60*1000))
      //计算相差秒数
      var second=min%(60*1000)
      //计算分钟数后剩余的毫秒数
      var seconds=Math.round(second/1000)
      if(year > 0){
        return year + "年前";
      }else if(month > 0){
        return month + "月前";
      }else if(days == 1){
        return '昨天';
      }else if(days == 2){
        return '前天';
      }else if(days > 0){
        return days + '天前';
      }else if(hours > 0){
        return hours + '小时前';
      }else if(minutes > 0){
        return minutes + '分钟前';
      }else if(seconds > 0){
        return seconds+'秒前';
      }else{
        return '1秒前';
      }
    }

  
}

Date.prototype.format = function (fmt) { //author: meizz 
  var o = {
      "M+": this.getMonth() + 1, //月份 
      "d+": this.getDate(), //日 
      "H+": this.getHours(), //小时 
      "m+": this.getMinutes(), //分 
      "s+": this.getSeconds(), //秒 
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
      "w+": this.getDay(), //星期，0为周日，1为周一，依次。。。
      "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}